/**
 * 创建人:  @author huangpeng    
 * 创建时间:  2022年09月12日 18:15
 * 项目名称:  lemon-blog-cloud
 * 文件名称:  IndexInfoServiceImpl
 * 文件描述:  @Description: 首页信息service实现类
 * 公司名称:  柠檬
 *
 * All rights Reserved, Designed By 柠檬 
 * @Copyright: 2022-2022
 *
 */
package com.lemon.blog.system.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.lemon.blog.system.constants.LemonConstants;
import com.lemon.blog.system.domain.BlTag;
import com.lemon.blog.system.domain.BlType;
import com.lemon.blog.system.mapper.BlBlogMapper;
import com.lemon.blog.system.mapper.BlCommentMapper;
import com.lemon.blog.system.mapper.BlTagMapper;
import com.lemon.blog.system.mapper.BlTypeMapper;
import com.lemon.blog.system.service.IndexInfoService;
import com.lemon.blog.system.service.feign.SystemFeignClient;
import com.lemon.common.core.constant.CacheConstants;
import com.lemon.common.core.text.Convert;
import com.lemon.common.core.utils.DateUtils;
import com.lemon.common.core.utils.StringUtils;
import com.lemon.common.core.utils.feign.FeignUtils;
import com.lemon.common.core.web.domain.AjaxResult;
import com.lemon.common.redis.service.RedisService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

import static java.util.Collections.sort;

/**
 * 包名称：com.lemon.blog.service.impl
 * 类名称：IndexInfoServiceImpl
 * 类描述：首页信息service实现类
 * 创建人：@author huangpeng
 * 创建时间：2022年09月12日 18:15
 */
@Service
public class IndexInfoServiceImpl implements IndexInfoService {

    public static final Logger logger = LoggerFactory.getLogger(IndexInfoServiceImpl.class);

    @Autowired
    private BlBlogMapper blogMapper;

    @Autowired
    private SystemFeignClient systemFeignClient;

    @Autowired
    private BlCommentMapper commentMapper;

    @Autowired
    private RedisService redisService;

    @Autowired
    private BlTypeMapper typeMapper;

    @Autowired
    private BlTagMapper tagMapper;

    @Override
    public AjaxResult queryPanelList() {
        Map<String, Object> resultMap = new HashMap<>();
        AjaxResult userResult = systemFeignClient.queryUserListByKey("");
        if (FeignUtils.feignStatus(userResult) && userResult.getData() != null) {
            // 获取用户数量
            resultMap.put("userCount", JSONArray.parseArray(JSON.toJSONString(userResult.getData())).size());
        } else {
            return AjaxResult.error("系统管理服务异常！");
        }
        AjaxResult baseUserResult = systemFeignClient.queryBaseUserListByKey("");
        if (FeignUtils.feignStatus(baseUserResult) && baseUserResult.getData() != null) {
            // 获取用户数量
            resultMap.put("baseUserCount", JSONArray.parseArray(JSON.toJSONString(baseUserResult.getData())).size());
        } else {
            return AjaxResult.error("系统管理服务异常！");
        }

        // 获取访客数
        String visitorCount = Convert.toStr(redisService.getCacheObject(CacheConstants.VISITOR_COUNT_KEY));
        // 获取文章数量、评论数量
        int blogCount = blogMapper.selectBlogCount();
        int commentCount = commentMapper.selectCommentCount();
        resultMap.put("blogCount", blogCount);
        resultMap.put("commentCount", commentCount);
        resultMap.put("visitorCount", StringUtils.isNotBlank(visitorCount) ? Integer.parseInt(visitorCount) : 0);
        return AjaxResult.success(resultMap);
    }

    @Override
    public AjaxResult blogContribute() {
        // 获取今天结束时间
        String endTime = DateUtils.getTime();
        // 获取365天前的日期
        Date temp = DateUtils.getDate(endTime, -365);
        String startTime = DateUtils.dateTimeToStr(temp);
        List<Map<String, Object>> blogContributeMap = blogMapper.queryBlogContributeCount(startTime, endTime);
        List<String> dateList = DateUtils.getDayBetweenDates(startTime, endTime);
        Map<String, Object> dateMap = new HashMap<>();
        for (Map<String, Object> itemMap : blogContributeMap) {
            dateMap.put(itemMap.get("date").toString(), itemMap.get("count"));
        }

        List<List<Object>> resultList = new ArrayList<>();
        for (String item : dateList) {
            int count = 0;
            if (dateMap.get(item) != null) {
                count = Integer.parseInt(dateMap.get(item).toString());
            }
            List<Object> objectList = new ArrayList<>();
            objectList.add(item);
            objectList.add(count);
            resultList.add(objectList);
        }
        Map<String, Object> resultMap = new HashMap<>(2);
        List<String> contributeDateList = new ArrayList<>();
        contributeDateList.add(startTime);
        contributeDateList.add(endTime);
        resultMap.put(LemonConstants.BLOG_KEY_CONTRIBUTE_DATE, contributeDateList);
        resultMap.put(LemonConstants.BLOG_KEY_CONTRIBUTE_COUNT, resultList);
        return AjaxResult.success(resultMap);
    }

    @Override
    public AjaxResult queryPieChartList() {
        Map<String, Object> resultMap = new HashMap<>();
        List<BlType> typeList = typeMapper.selectBlTypeList(new BlType());
        List<Map<String, Object>> resultType = new ArrayList<>();
        for (BlType blType : typeList) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", blType.getId());
            map.put("name", blType.getTypeName());
            map.put("value", blType.getTypeBlogCount());
            resultType.add(map);
        }
        List<BlTag> tagList = tagMapper.selectBlTagList(new BlTag());
        Collections.sort(tagList);
        List<Map<String, Object>> resultTag = new ArrayList<>();
        for (BlTag blTag : tagList) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", blTag.getId());
            map.put("name", blTag.getTagName());
            map.put("value", blTag.getTagBlogCount());
            resultTag.add(map);
        }

        resultMap.put("typeList", resultType);
        resultMap.put("tagList", resultTag);
        return AjaxResult.success(resultMap);
    }
}
